const g = getApp().globalData
const util = g.util
const MPLogin = require('../../utils/MPLogin.js')

Page({
    data: {
        deviceList: [],
        page: 1,
        pageSize: 10,
        total: 0,
        totalPage: 0,

        bottomHidden: true,
        gifShow: true,
        hasData: false,
    },

    onLoad: function () {
        if (g.userInfo) {
            // 已登录
            util.showLoading()
            this.getDeviceList()
        } else {
            MPLogin.login(res => {
                if (res) {
                    // 已授权
                    util.showLoading()
                    this.getDeviceList()
                } else {
                    wx.redirectTo({
                        url: '../index/index',
                    })

                }
            })

        }
    },

    onPullDownRefresh() {
        wx.stopPullDownRefresh()
        if (this.requestBack) {
            util.showLoading()
            this.setData({
                page: 1
            })
            this.getDeviceList()
        }
    },

    onReachBottom() {
        if (this.requestBack) {
            this.setData({
                bottomHidden: false,
                gifShow: this.data.hasData
            })
            if (this.data.hasData) {
                this.getDeviceList()
            }
        }
    },

    //获取设备列表
    getDeviceList() {
        let url = `switchs/list?page=${this.data.page}&page_size=${this.data.pageSize}`
        this.requestBack = false
        util.httpGet({
            url: url,
            header: { authorization: g.userInfo.token },
            fn: res => {
                if (res == 'refreshToken') {
                    this.getDeviceList()
                    return
                }
                console.log('list', res)
                let result = res.data.result
                let currList = this.data.page == 1 ? [] : this.data.deviceList
                let resList = this.processData(result.rows)
                let concatList = currList.concat(resList)
                this.setData({
                    deviceList: concatList,
                    total: result.total,
                    totalPage: result.total_page,
                    hasData: concatList.length != result.total,
                    page: ++this.data.page,
                    bottomHidden: true
                })
            },
            comFn: com => {
                this.requestBack = true
                util.hideLoading()
            }
        })
    },

    //处理返回的数据
    processData(data) {
        for (let i in data) {
            switch (data[i].status) {
                case 0:
                    data[i].statusText = '未联网'
                    data[i].opened = false
                    break;
                case 1:
                    data[i].statusText = '开启'
                    data[i].opened = true
                    break;
                case 2:
                    data[i].statusText = '关闭'
                    data[i].opened = false
                    break;
            }

        }
        return data
    },

    //跳转到详情页
    goDetail(e) {
        let id = e.currentTarget.dataset.id
        let name = e.currentTarget.dataset.name

        wx.navigateTo({
            url: `../detail/detail?id=${id}&name=${name}`,
        })
    },

    // 切换状态
    switchChange(e) {
        console.log(e)
        let status = e.detail.value
        let id = e.currentTarget.dataset.id
        let url = `switchs/onOff`
        let body = {
            status: status ? 1 : 2, //0未开启，1开启，2关闭
            id: id,
        }
        let toast = body.status == 1 ? '开启中...' : '关闭中...'
        util.showLoading(toast)
        util.httpPost({
            url: url,
            body: body,
            header: { authorization: g.userInfo.token },
            fn: res => {
                if (res == 'refreshToken') {
                    this.switchChange(e)
                    return
                }
                wx.showToast({
                    title: res.data.msg,
                })
                let deviceList = this.data.deviceList

                for (let i in deviceList) {
                    if (deviceList[i].id == id) {
                        deviceList[i].status = res.data.result.status
                    }
                }
                deviceList = this.processData(deviceList)
                this.setData({
                    deviceList
                })
            },
            failFn: failRes => {
                setTimeout(() => {
                    wx.showToast({
                        title: failRes.data.msg,
                    })
                }, 500)
                //状态更新失败需要刷新数据
                this.getDeviceList()
            }
        })
    },

    //阻止冒泡
    stopBubble() {

    },


})
